package com.gz.WeChatSys.controller;

import com.gz.WeChatSys.service.WxMsgService;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;
import me.chanjar.weixin.mp.util.xml.XStreamTransformer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author lydia
 * @date 2018/10/15 上午10:11
 * @discription
 */
@RestController
@RequestMapping("/pushMsg")
public class PushMsgController {
    private final static Logger logger = LoggerFactory.getLogger(PushMsgController.class);

    /*@Resource
    private WxMpConfigStorage wxMpConfigStorage;*/

    @Resource
    private WxMsgService wxMsgService;

    @Resource
    private WxMpService wxMpService;

    @RequestMapping("/wx/response")
    @ResponseBody
    public void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
        InputStream is = request.getInputStream();
        WxMpXmlMessage wxMpXmlMessage = XStreamTransformer.fromXml(WxMpXmlMessage.class, is);

        String fromUser = wxMpXmlMessage.getFromUser();
        String touser = wxMpXmlMessage.getToUser();
        if (wxMpXmlMessage.getEventKey().equals("bt1_1")) {
            //创建消息文本
            WxMpXmlOutTextMessage text = WxMpXmlOutTextMessage.TEXT()
                    .toUser(fromUser)
                    .fromUser(touser)
                    .content("点击了河道信息")
                    .build();

            String xml = text.toXml();
            PrintWriter out = null;
            try {
                out = response.getWriter();
                out.print(xml);
            } catch (IOException e) {
                out.close();
                out = null;
                e.printStackTrace();
            }
            out.close();
            out = null;
        }
    }

    @RequestMapping("/wx/push")
    @ResponseBody
    public void wxPush(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //微信服务器get传递的参数
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");

        boolean flag = false;
        String retMsg = echostr;
        if (StringUtils.isNoneBlank(echostr)) {
            flag = wxMpService.checkSignature(timestamp, nonce, signature);
        } else {
            InputStream is = request.getInputStream();
            WxMpXmlMessage wxMpXmlMessage = XStreamTransformer.fromXml(WxMpXmlMessage.class, is);
            WxMpXmlOutMessage wxMpXmlOutMessage = wxMsgService.handlerMsg(wxMpService, wxMpXmlMessage);
            if (wxMpXmlOutMessage != null) {
                flag = true;
//                retMsg = wxMpXmlOutMessage.toEncryptedXml(wxMpService.getWxMpConfigStorage());//密文
                retMsg = wxMpXmlOutMessage.toXml();//明文
//                logger.error(retMsg);
            } else {
                logger.error("未返回数据");
            }
        }
        PrintWriter out = response.getWriter();
        if (flag) {
            out.print(new String(retMsg.getBytes(),"ISO-8859-1"));
        }
        out.close();
    }

}
